<template>
	<view v-if="show">
		<view v-show="show" :style="{ top: offsetTop + 'px',bottom: getBottom }" class="uni-mask" @click="hide" @touchmove.stop.prevent="moveHandle" />
		<view v-show="show" :class="'uni-popup-' + position + ' ' + 'uni-popup-' + mode"  class="uni-popup" :style="{ 'padding-bottom': (position=='full' ? paddingBottom:0), bottom: (position=='full' ? getBottom:''),'height':(height!='' && position=='full' ? height : '') }">
			<view class="uni-close-top" v-if="title!=''">
				<text class="popup-title">{{title}}</text>
				<view v-if="position === 'full'" style="color:#000000;font-size:28upx;position:absolute;right:40upx;top:20upx;line-height:1;" @click="closeMask">
				  <!-- <image src="./icon_close.png" style="width:16px;height:16px;"></image> -->
				  <image src="../../static/icon_close.png" style="width:48upx;height:48upx;"></image>
				</view>
			</view>
			<!-- <view style="height:30upx;"></view> -->
			{{ msg }}
			<view v-if="position === 'middle' && mode === 'insert' && circle" :class="'uni-popup-info ' + cssClass" >
				<slot />
			</view>
			<slot v-else />
			
			<view v-if="showBottom && position === 'middle' && mode === 'insert'" :class="{
			  'uni-close-bottom': buttonMode === 'bottom',
			  'uni-close-right': buttonMode === 'right'
			}" class="uni-theme-color" @click="closeMask" >  
			 <!-- 关闭 -->
			  <image v-if="closeText==''" src="../../static/icon_close.png" style="width:32px;height: 32px;"></image>
			  <text v-else>{{closeText}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'UniPopup',
		props: {
			/**
			 * 页面显示
			 */
			showBottom: {
				type: Boolean,
				default: true
			},
			show: {
				type: Boolean,
				default: false
			},
			cssClass: {
				type: String,
				default: ''
			},
			/**
			 * 对齐方式
			 */
			position: {
				type: String,
				// top - 顶部， middle - 居中, bottom - 底部
				default: 'middle'
			},
			/**
			 * 显示模式
			 */
			mode: {
				type: String,
				default: 'insert'
			},
			/**
			 * 关闭文本
			 */
			closeText: {
				type: String,
				default: ''
			},
			/**
			 * 标题
			 */
			title: {
				type: String,
				default: ''
			},
			/**
			 * 额外信息
			 */
			msg: {
				type: String,
				default: ''
			},
			circle:{
				type: Boolean,
				default: true
			},
			/**
			 * h5遮罩是否到顶
			 */
			h5Top: {
				type: Boolean,
				default: true
			},
			buttonMode: {
				type: String,
				default: 'bottom'
			},
			bottom: {
				type:String,
				default:"0px"
			},
			height: {
				type: String,
				default: ''
			},
			paddingBottom: {
				type: String,
				default: "0px"
			}
		},
		data() {
			return {
				offsetTop: 0
			}
		},
		watch: {
			h5Top(newVal) {
				if (newVal) {
					this.offsetTop = 44
				} else {
					this.offsetTop = 0
				}
			}
		},
		created() {
			let offsetTop = 0
			// #ifdef H5
			if (!this.h5Top) {
				offsetTop = 44
			} else {
				offsetTop = 0
			}
			// #endif
			this.offsetTop = offsetTop
		},
		methods: {
			hide() {
				if (this.mode === 'insert' && this.position === 'middle') return
				this.$emit('hidePopup')
			},
			closeMask() {
				if (this.mode === 'insert') {
					this.$emit('hidePopup')
				}
			},
			moveHandle() {}
		},
		computed: {
			getBottom(){
				if(this.bottom.toString().lastIndexOf("px")>-1){
					return this.bottom;
				}else{
					return uni.upx2px(this.bottom) + 'px';
				}
			},
			getStyle() {
				if(this.position==='full')
				{
					return { "padding-bottom": this.paddingBottom, "bottom": this.getBottom, "height":(this.height=="" ? "":this.height) };					
				}
			}
        }
	}
</script>

<style scoped>
	.popup-title{
		width: 80%;
		margin-left: 10%;
		margin-right: 10%;
		font-size: 32upx;
		line-height: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
	}
	.uni-popup-full /deep/ uni-scroll-view,.uni-popup-full /deep/ scroll-view{
		height: calc(100% - 124upx);
	}
	.uni-mask {
		position: fixed;
		z-index: 888;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background-color: rgba(0, 0, 0, 0.8);
	}

	.uni-popup {
		position: fixed;
		z-index: 889;
		background-color: #ffffff;
	}

	.uni-popup-middle {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}


	.uni-popup-slot {
		width: 100%;
		background-color: transparent;
	}
	
	.uni-popup-middle.uni-popup-insert {
		/* min-width: 400upx;
		min-height: 400upx; */
		max-width: 100%;
		max-height: 80%;
		/* transform: translate(-50%, -65%); */
		background: none;
		box-shadow: none;
	}

	.uni-popup-info{
		width: 600upx;
		border-radius: 5px;
		background-color: #fff;
		padding: 30upx 30upx 126upx 30upx;
		/* min-height: 240upx; */
	}

	.uni-popup-middle.uni-popup-fixed {
		border-radius: 10upx;
		padding: 30upx;
	}

	.uni-close-right {
		position: absolute;
		bottom: 0px;
		text-align: center;
		border-radius: 50%;
		color: #f5f5f5;
		font-size: 60upx;
		font-weight: bold;
		opacity: 0.8;
		z-index: -1;
	}

	.uni-close-bottom {
		position: absolute;
		bottom: 0px;
		text-align: center;
		font-weight: bold;
		background-color: #fff;
		height: 96upx;
		line-height: 96upx;
		font-size: 16px;
		left: 0px;
		right: 0px;
		border-radius: 0px 0px 5px 5px;
	}
	.uni-close-bottom:after {
		content: " ";
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		height: 1px;
		border-top: 1px solid #d5d5d6;
		color: #d5d5d6;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
	}

	.uni-close-right {
		right: -60upx;
		top: -80upx;
	}


	.uni-popup-top {
		top: 0;
		left: 0;
		width: 100%;
		height: 100upx;
		line-height: 100upx;
		text-align: center;
	}

	.uni-popup-bottom {
		left: 0;
		bottom: 0;
		width: 100%;
		min-height: 100upx;
		line-height: 100upx;
		text-align: center;
	}
	
	.uni-popup-full {
		position: fixed;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
/* 		height: auto;
		max-height: 80%; */
		bottom: 0px;
		left: 0px;
		right: 0px;
		padding: 0px 10px 10upx 10px;
		border-top-left-radius: 10px;
	    border-top-right-radius: 10px;
	}
	.uni-close-top{
		position: relative;
		padding: 30upx;
		-webkit-border-image: url() 2 stretch;
		border-style: solid;
		border-width: 0 0 1px;
		width: 100%;
		text-align: center;
	 } 
	 
	 .popup-image{
	 	height: 100%;
	 	background-size: 100% 100%;
		padding-bottom: 15px;
	 }
</style>
